import {hex_md5} from './MD5';

export const Utils = {
    trim	:	function(n){
        if(n==null)
            return null ;
        n = n + "";
        return n.replace(/(^\s+)|\s+$/g,'');
    },
    encodeHexUrl	:	function(s) {
        if(s==null)
            return null ;

        if(s==='')
            return '';

        return '=u='+ this.utf8UrlEncode0(s);
    },
    decodeHexUrl	:	function(s){
        if(!s)
            return s ;

        if(s.indexOf("=h=") !== 0 && s.indexOf("=u=") !== 0)
            return s;

        s = s.substr(3);
        return this.utf8UrlDecode0(s);
    },
    md5	:	function(v){
        return hex_md5(v);
    },
    utf8UrlEncode	:	function(v) {
        return encodeURIComponent(v);
    },

    utf8UrlDecode	:	function(v) {
        return decodeURIComponent(v);
    },
    utf8UrlEncode0	:	function(v) {
        return escape(encodeURIComponent(v)) ;
    },

    utf8UrlDecode0	:	function(v) {
        return decodeURIComponent(unescape(v));
    },
    getSize			:	function(size){
        if(isNaN(size)){
            return "err";
        }
        var K = 1024;
        var M = 1024 * 1024;
        if (size > 10 * M) {
            return (size / M) + "M";
        }
        if (size > 10 * K) {
            return (size / K) + "K";
        }
        return size + "b";
    },
    ajaxPostSync		:	function(url,sendstr,cb){
        var __req = null;
        if (window.XMLHttpRequest){
            __req = new XMLHttpRequest();
        } else if (window.ActiveXObject){
            __req = new window.ActiveXObject("Microsoft.XMLHTTP");
        }
        if(__req){
            __req.onreadystatechange = function(){
                if(4 === __req.readyState){
                    var res = __req.responseText;
                    try{
                        res = JSON.parse(__req.responseText);
                    }catch(E){}
                    if(200 === __req.status){
                        if('function' === typeof cb){
                            cb(res);
                        }
                    }else{
                    }
                }
            };
            __req.open("POST", url, false);
            __req.setRequestHeader("Content-type","application/json");
            __req.send(sendstr);
        }
    },
    ajaxPost		:	function(url,sendstr,cb){
        var __req = null;
        if (window.XMLHttpRequest){
            __req = new XMLHttpRequest();
        } else if (window.ActiveXObject){
            __req = new window.ActiveXObject("Microsoft.XMLHTTP");
        }
        if(__req){
            __req.onreadystatechange = function(){
                if(4 === __req.readyState){
                    var res = __req.responseText;
                    try{
                        res = JSON.parse(__req.responseText);
                    }catch(E){}
                    if(200 === __req.status){
                        if('function' === typeof cb){
                            cb(res);
                        }
                    }else{
                    }
                }
            };
            __req.open("POST", url, true);
            __req.setRequestHeader("Content-type","application/json");
            __req.send(sendstr);
        }
    },
    ajaxPostRaw	:	function(url,sendstr,cb){
        var __req = null;
        if (window.XMLHttpRequest){
            __req = new XMLHttpRequest();
        } else if (window.ActiveXObject){
            __req = new window.ActiveXObject("Microsoft.XMLHTTP");
        }
        if(__req){
            __req.onreadystatechange = function(){
                if(4 === __req.readyState){
                    var res = __req.responseText;
                    if(200 === __req.status){
                        if('function' === typeof cb){
                            cb(res);
                        }
                    }else{
                    }
                }
            };
            __req.open("POST", url, true);
            __req.send(sendstr);
        }
    },
    ajaxGet		:	function(url,cb){
        var __req = null;
        if (window.XMLHttpRequest){
            __req = new XMLHttpRequest();
        } else if (window.ActiveXObject){
            __req = new window.ActiveXObject("Microsoft.XMLHTTP");
        }
        if(__req){
            __req.onreadystatechange = function(){
                if(4 === __req.readyState){
                    if(200 === __req.status){
                        if('function' === typeof cb){
                            cb(__req.responseText);
                        }
                    }else{
                    }
                }
            };
            __req.open("GET", url, true);
            __req.send(null);
        }
    },
    createPageRequest	:	function(curPage,pageSize,orderByField,orderByType){
        if(isNaN(curPage) || curPage < 1){
            curPage = 1;
        }
        if(isNaN(pageSize) || pageSize < 1){
            pageSize = 10;
        }
        var o = {
            "curPage" 	:	curPage,
            "pageSize"	:	pageSize
        };

        if(orderByField){
            o.orderByField = orderByField;
        }
        if(orderByType){
            o.orderByType = orderByType
        }
        return o;
    },
    leftId	:	function(id,n){
        var idx = id.lastIndexOf("_");
        var v = id.substr(idx + 1);
        v = parseInt(v,10) + n;
        return id.substr(0,idx + 1) + v;
    },
    topId	:	function(id,n){
        var ch = id.split("_");
        var v = ch[ch.length -2];
        v = parseInt(v,10) + n;

        var res = "";
        for(var i = 0 ; i < ch.length; i ++){
            if(0 !== i){
                res += "_";
            }
            if(i === ch.length - 2){
                res +=  v;
            }else{
                res += ch[i];
            }
        }
        return res;
    },
    formatNum00 : function(v){
        v = parseInt(v,10);
        if(v > -1 && v < 10)
            return "0" + v;
        return v;
    },
    localDate	:	function(){
        var date = new Date();
        var nyear = date.getFullYear();
        var nmonth = date.getMonth() + 1;
        var nday = date.getDate();
        var nval = nyear + "-" + Utils.formatNum00(nmonth) + "-"
            + Utils.formatNum00(nday);
        return nval;
    },
    localDateTime	:	function(){
        var date = new Date();
        var nyear = date.getFullYear();
        var nmonth = date.getMonth() + 1;
        var nday = date.getDate();
        var nhour = date.getHours();
        var nminute = date.getMinutes();
        var nsecond = date.getSeconds();
        var nval = nyear + "-" + Utils.formatNum00(nmonth) + "-"
            + Utils.formatNum00(nday) + " " + Utils.formatNum00(nhour) + ":"
            + Utils.formatNum00(nminute) + ":" + Utils.formatNum00(nsecond);
        return nval;
    },
    dateCompare	:	function(firstDate,secondDate){
        return Date.UTC(firstDate.getFullYear(),firstDate.getMonth(),
                firstDate.getDate(),firstDate.getHours(),firstDate.getMinutes()
                ,firstDate.getSeconds()) -
            Date.UTC(secondDate.getFullYear(),secondDate.getMonth(),
                secondDate.getDate(),secondDate.getHours(),secondDate.getMinutes()
                ,secondDate.getSeconds());
    },
    dateParse	:	function(v){
        var date = new Date();
        if(!v || 'string' !== typeof v){
            date.setFullYear(1970);
            date.setMonth(0);
            date.setDate(1);
            date.setHours(0);
            date.setMinutes(0);
            date.setSeconds(0);
            return date;
        }
        var d = v.split(" ");
        if(1 === d.length){
            let ymd = d[0].split("-");
            date.setFullYear(parseInt(ymd[0],10));
            date.setMonth(parseInt(ymd[1],10) - 1);
            date.setDate(parseInt(ymd[2],10));
        }else if(2 === d.length){
            let ymd = d[0].split("-");
            date.setFullYear(parseInt(ymd[0],10));
            date.setMonth(parseInt(ymd[1],10) - 1);
            date.setDate(parseInt(ymd[2],10));
            var hms = d[0].split("-");
            date.setHours(parseInt(hms[0],10));
            date.setMinutes(parseInt(hms[1],10));
            date.setSeconds(parseInt(hms[2],10));
        }
        return date;
    },
    dateLong2Short:function(d){
        if(d){
            var i = d.indexOf(" ");
            if(i > 0){
                return d.substring(0,i);
            }
            return d;
        }
        return d;
    }
};